﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyProj.MVCProj3.BLL;
using MyProj.MVCProj3.Model;

namespace MyProj.MVCProj3.UI.Main.Controllers
{
    public class UserInfoController : Controller
    {
        //
        // GET: /UserInfo/

        UserInfoService bll = new UserInfoService();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Delete(string IDs)
        {
            string[] strIDs = IDs.Split(',');
            foreach (var item in strIDs)
            {
                UserInfo user=GetSingleUserInfo(int.Parse(item));
                bll.DeleteEntity(user);
            }
            return Content("true");    
        }

        public ActionResult Edit(int ID)
        {
            UserInfo user = GetSingleUserInfo(ID);
            ViewData.Model = user;
            return View();
        }

        private UserInfo GetSingleUserInfo(int ID)
        {
            UserInfo user = bll.LoadEntities(u => u.ID == ID).First();
            return user;
        }
        //GPR get post redriect
        [HttpPost]
        public ActionResult Edit(int ID,UserInfo postedUser)
        {
            UserInfo user = GetSingleUserInfo(ID);
            user.Age = postedUser.Age;
            user.NickName = postedUser.NickName;
            user.Email = postedUser.Email;
            user.Phone=postedUser.Phone;
            user.Address=postedUser.Address;
            user.UPwd=postedUser.UPwd;
            if (bll.UpdateEntity(user) == true)
            {
                return Content("true");
            }
            else
            {
                return Content("false");  
            }

        }
        public ActionResult LoadAllUserInfo() 
        {
            
            int pageSize=10;
            if (int.TryParse(Request["rows"], out pageSize) == false)
                pageSize=10;
            int pageIndex = 10;
            if (int.TryParse(Request["page"], out pageIndex) == false)
                pageIndex = 1;
            int total;
            var rows = bll.LoadPageEntities<int>(pageSize, pageIndex,out total,u=>true,u=>u.ID,true);
            //只取出前台需要的数据，避免json中循环引用异常
            return Json(new { total = total, rows = from u in rows select new { u.ID,u.UserName ,u.Address,u.Age,u.Email} }, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult AddUser(FormCollection collection)
        {
            if (collection == null || collection.Count < 1)
            {
                return Content("false");
            }
            var user = new MyProj.MVCProj3.Model.UserInfo()
            {
                UserName = collection["UserName"],
                UPwd = collection["UPwd"],
                Address = collection["Address"],
                Age = short.Parse(collection["Age"]),
                Email = collection["Email"],
                /////////
                DelFlag = 0,
                ErrorTime = 0,
                Gender = 1,
                LastLoginDate = DateTime.Now,
                LastLoginIp = Request.UserHostAddress,
                NickName = "nick",
                Phone = "1111",
                SubTime = DateTime.Now
            };

            if (bll.AddEntity(user) == true)
            {
                return Content("true");
            }
            else
            {
                return Content("false");
            }
        }
    }
}
